<template>
  <div class="padding">
    <el-card>
      <div slot="header" class="flex align-center justify-between">
        <span class="title-icon">文件列表</span>
      </div>
      <el-table size="mini" :data="fileList">
        <el-table-column
          type="index"
          width="44"
          label="序号"
          align="center"
        ></el-table-column>
        <ex-table-column prop="fileTypeId" label="文件类型">
          <template slot-scope="scope">
            {{ publicData.fileTypeMap[scope.row.fileTypeId] }}
          </template>
        </ex-table-column>
        <ex-table-column
          min-width="250"
          prop="fileName"
          label="文件名"
        ></ex-table-column>

        <ex-table-column label="操作">
          <template slot-scope="scope">
            <i
              class="el-icon-download pointer margin-lr-sm"
              @click="down(scope.row)"
            ></i>
            <i class="el-icon-delete pointer" @click="delFile(scope)"></i>
          </template>
        </ex-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { queryWaybillFileList, deleteWaybillFile } from '@/api/abroadWaybill'
import { mapGetters } from 'vuex'
import golablData from '@/utils/config'
console.log('golablData :>> ', golablData)
export default {
  props: ['waybillId'],
  data() {
    return {
      fileList: [],
      fileUrl: golablData.fileUrl
    }
  },
  computed: {
    ...mapGetters(['publicData'])
  },
  mounted() {
    this.init()
  },
  methods: {
    async init() {
      const data = await queryWaybillFileList({
        waybillId: parseInt(this.waybillId)
      })
      if (data) {
        if (data instanceof Array) {
          this.fileList = data
        }
      } else {
        this.$message.error('获取失败！')
      }
    },
    down(s) {
      if (s.fileUrl) {
        window.open(this.fileUrl + s.fileUrl)
      } else {
        this.$message.error('文件路径有误！')
      }
    },
    delFile(s) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.saveDel(s)
        })
        .catch(() => {})
    },
    async saveDel(s) {
      const { code, message } = await deleteWaybillFile({ id: s.row.id })
      if (code === 200) {
        this.$message.success('删除成功！')
        this.init()
      } else {
        this.$message.error('删除失败：' + message)
      }
    }
  }
}
</script>

<style></style>
